/**
 * 
 */
package cn.biplam.front.template.complex;

import cn.biplam.common.paramters.SysName;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.htmlscript.script.LayoutScript;
import cn.biplam.front.htmlscript.script.PopupScript;
import cn.biplam.front.template.Template;
import cn.biplam.front.template.base.HiddenParams;

/**
 * 数据操作组件调用模板(用于复用组件,将请求组件返回的HtmlWindow置入Prompt窗口使用)<BR>
 * 由Pr请求发起,调用指定的action,传递所需参数,<BR>
 * 使用方法<BR>
 * 1.Pr请求到自己提供的组件接口<BR>
 * 2.在接口响应方法里,创建本模板(传入调用actionid、提交actionid、数据主键Id)<BR>
 * 3.putParams放入需要提交给"调用action"的参数<BR>
 * 4.putTransParam放入需要提交给"提交action"的参数(通过sp的附加参数列表传递)<BR>
 * 5.return 本模板.get()<BR>
 * <Font color=Red>调用使用本模板的组件,必须是Pr请求(弹出Prompt窗口)</font>
 * @author 	WangSong 2009-10-27 上午11:16:35
 * @version 1.0
 */
public class DataPrompt implements Template{

	/** 用于中转提交的参数容器ID*/
	private final String cID="DataPrompt";

	/** 请求的actionId*/
	private String reqActionId;

	/** 提交的actionId*/
	private String submitActionId;

	/** 数据主键ID*/
	private String reqRecordId;
	
	/** 提交的数据主键ID*/
	private String submitRecordId=null;

	/** 提交的页面对象ID*/
	private String submitContainerId=null;

	/** 提交参数模板*/
	private HiddenParams params;
	
	private KvMap<String, String> transParams;


	/**
	 * 提交容器为PromptContent,提交和请求使用同一个recordId
	 * @param reqActionId		请求的actionId
	 * @param submitActionId	提交的actionId
	 * @param recordId			请求的recordId
	 * @throws ErrorMessage
	 */
	public DataPrompt(String reqActionId,String submitActionId,String reqRecordId) throws ErrorMessage{
		this.reqActionId=reqActionId;
		this.submitActionId=submitActionId;
		this.reqRecordId=reqRecordId;
		this.params = new HiddenParams(this.cID,reqRecordId);
		this.transParams=new KvMap<String, String>();
	}
	
	/**
	 * 提交容器为返回页面对象,其包含隐藏rid
	 * @param reqActionId			请求的actionId
	 * @param submitActionId		提交的actionId
	 * @param reqRecordId			请求的recordId
	 * @param submitContainerId		提交页面容器Id
	 * @throws ErrorMessage
	 */
	public DataPrompt(String reqActionId,String submitActionId,String reqRecordId,String submitContainerId) throws ErrorMessage{
		this(reqActionId, submitActionId, reqRecordId);
		this.submitContainerId=submitContainerId;
	}
	
	/**
	 * 提交容器为返回页面对象,忽略其隐藏rid,使用sp附加参数传递rid
	 * @param reqActionId			请求的actionId
	 * @param submitActionId		提交的actionId
	 * @param reqRecordId			请求的recordId
	 * @param submitContainerId		提交页面容器Id
	 * @param submitRecordId		提交的recordId
	 * @throws ErrorMessage
	 */
	public DataPrompt(String reqActionId,String submitActionId,String reqRecordId,String submitContainerId,String submitRecordId) throws ErrorMessage{
		this(reqActionId, submitActionId, reqRecordId,submitContainerId);
		this.submitRecordId=submitRecordId;
	}


	public HtmlWindow get() throws ErrorMessage {
		//生成参数容器页面对象
		HtmlWindow div=params.get();
		div.setStyleSize(1, 1);
		//绑定提交脚本(请求目标action,返回内容放入PromptContent)
		String script = AjxScript.getMs(div.getID(),this.reqActionId,this.reqRecordId,SysName.PromoptContentID);
		div.bind(script);

		//设置Prompt窗口提交按钮
		div.bind(SysName.PromptSubmitID,Window_Event.OnClick,PopupScript.cancel(SysName.PromptID));
		if(this.submitContainerId==null){//提交容器为PromoptContent
			div.bind(SysName.PromptSubmitID,Window_Event.OnClick,
					AjxScript.getSpParamsNoRefresh(SysName.PromoptContentID,this.submitActionId,this.reqRecordId,this.transParams)
			);
		}else if(this.submitRecordId==null){//提交容器含有隐藏rid
			div.bind(SysName.PromptSubmitID,Window_Event.OnClick,
					AjxScript.getSpParamsNoRefresh(this.submitContainerId,this.submitActionId,this.transParams)
			);
		}else{//提交容器没有隐藏rid,使用传入的提交rid
			div.bind(SysName.PromptSubmitID,Window_Event.OnClick,
					AjxScript.getSpParamsNoRefresh(this.submitContainerId,this.submitActionId,this.submitRecordId,this.transParams)
			);
		}

		div.bind(LayoutScript.PromptShow());
		return div;
	}

	/**
	 * 放入一个需要提交给请求组件的数据参数
	 * @param name
	 * @param value
	 * @throws ErrorMessage
	 */
	public void putParam(String name,String value) throws ErrorMessage{
		this.params.put(name, value);
	}
	
	/**
	 * 放入一个需要传递给提交页面的数据参数
	 * @param name
	 * @param value
	 * @throws ErrorMessage
	 */
	public void putTransParam(String name,String value) throws ErrorMessage{
		this.transParams.put(name, value);
	}

}
